import {userLogin} from '../api/user'
import Vue from 'vue'
import router from '../router'
export default{
    namespaced:true,  //开启命名空间
    state:()=>({
        userInfo:null
    }),
    mutations:{
        userInfoMut(state,info){
            state.userInfo = info
        }
    },
    actions:{
        userLoginAct(context,obj){ //发起登录请求的异步action
            userLogin(obj).then(res=>{
                console.log(res.data);
                let {data,code,msg} = res.data
                console.log(data);
                // console.log(code);
                if(code==1){
                    console.log(data);
                    context.commit('userInfoMut',data)  //将数据存储至状态机
                    localStorage.setItem('userInfo',JSON.stringify(data)) //本地存储
                    Vue.prototype.$message({
                        type:'success',
                        message:msg
                    })
                    router.push('/myspec')
                }
                if(code==0){
                    Vue.prototype.$message({
                        type:'error',
                        message:msg
                    })
                }
            })
        }
    }
}